package cn.edu.njuit.springbootfilmreview.controller;

import cn.edu.njuit.springbootfilmreview.dto.MovieInfo;
import cn.edu.njuit.springbootfilmreview.dto.WantInfo;
import cn.edu.njuit.springbootfilmreview.dto.WorksInfo;
import cn.edu.njuit.springbootfilmreview.service.WorksInfoService;
import cn.edu.njuit.springbootfilmreview.service.impl.BaiduAiService;
import cn.edu.njuit.springbootfilmreview.utils.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.Optional;
import java.util.UUID;

import static cn.edu.njuit.springbootfilmreview.utils.ResultCode.*;

/**
 * @Author guzy
 * @Description 作品接口
 * @Date 2022/6/5 10:43
 **/

@RestController
@RequestMapping("/api/works-info")
@Api(value = "作品接口", tags = "作品接口")
public class WorksInfoController {


    @Autowired
    BaiduAiService baiduAiService;

    @Autowired
    WorksInfoService worksInfoService;

    @ApiImplicitParams({
            @ApiImplicitParam(value = "第几页", name = "currentPage", dataType = "int", required = true),
            @ApiImplicitParam(value = "当前页多少条数据", name = "pageSize", dataType = "int", required = true)}
    )
    @ApiOperation(value = "分页获取所有作品")
    @GetMapping("/getAllWorksInfo/{currentPage}/{pageSize}")
    public Result getPageAllWorksInfo(
            @PathVariable Integer currentPage,
            @PathVariable int pageSize) {
        if (currentPage == null || String.valueOf(pageSize) == null) {
            return Result.failure(PARAM_NOT_COMPLETE);
        }
        IPage<WorksInfo> worksInfoIPage = worksInfoService.getPageAllWorksInfo(currentPage, pageSize);
        if (worksInfoIPage.getRecords().size() > 0) {
            return Result.success(worksInfoIPage);
        } else {
            return Result.failure(RESULT_CODE_DATA_NONE);
        }
    }

    @ApiImplicitParams({
            @ApiImplicitParam(value = "token", name = "token", dataType = "String", required = true),
            @ApiImplicitParam(value = "用户id", name = "userId", dataType = "Long", required = true),
            @ApiImplicitParam(value = "作品名称", name = "worksName", dataType = "String", required = true),
            @ApiImplicitParam(value = "作品照片", name = "imageUrl", dataType = "__file", required = true),
            @ApiImplicitParam(value = "想法", name = "text", dataType = "String", required = true)})
    @ApiOperation(value = "添加作品")
    @PostMapping("/add")
    public Result addWorksInfo(
            @RequestParam Long userId,
            @RequestParam String worksName,
            @RequestParam MultipartFile imageUrl,
            @RequestParam String text,
            @RequestParam String token) {

        //随机生成一个uuid
        String fileKey = UUID.randomUUID().toString();
        //获取文件名
        String fileName = imageUrl.getName();
        if (fileName.indexOf(".") > 0) {
            //获取后缀名
            String extName = fileName.substring(fileName.lastIndexOf(".") - 1);
            fileKey += extName;
        }

        try {
            //将照片存入百度BOS
            //获取存放后照片网络地址
            baiduAiService.putFile(fileKey, imageUrl.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }

        WorksInfo worksInfo = new WorksInfo();
        worksInfo.setWorksName(worksName);
        worksInfo.setPublishTime(new Date());
        worksInfo.setImageUrl("https://web-lesson.bj.bcebos.com/guzy-bos/" + fileKey);
        worksInfo.setText(text);
        worksInfo.setUserId(userId);

        boolean b = worksInfoService.addWorkInfo(worksInfo);
        if (b) {
            return Result.success("作品添加成功");
        } else {
            return Result.failure("作品添加失败");
        }
    }


    @ApiImplicitParams({
            @ApiImplicitParam(value = "token", name = "token", dataType = "String", required = true),
            @ApiImplicitParam(value = "id", name = "id", dataType = "Long", required = true),
            @ApiImplicitParam(value = "用户id", name = "userId", dataType = "Long", required = true),
            @ApiImplicitParam(value = "作品名称", name = "worksName", dataType = "String", required = true),
            @ApiImplicitParam(value = "作品照片", name = "imageUrl", dataType = "__file", required = true),
            @ApiImplicitParam(value = "想法", name = "text", dataType = "String", required = true)})
    @ApiOperation(value = "修改作品")
    @PostMapping("/update")
    public Result addWorksInfo(
            @RequestParam Long id,
            @RequestParam Long userId,
            @RequestParam String worksName,
            @RequestParam MultipartFile imageUrl,
            @RequestParam String text,
            @RequestParam String token) {

        //随机生成一个uuid
        String fileKey = UUID.randomUUID().toString();
        //获取文件名
        String fileName = imageUrl.getName();
        if (fileName.indexOf(".") > 0) {
            //获取后缀名
            String extName = fileName.substring(fileName.lastIndexOf(".") - 1);
            fileKey += extName;
        }

        try {
            //将照片存入百度BOS
            //获取存放后照片网络地址
            baiduAiService.putFile(fileKey, imageUrl.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }

        WorksInfo worksInfo = new WorksInfo();
        worksInfo.setId(id);
        worksInfo.setWorksName(worksName);
        worksInfo.setPublishTime(new Date());
        worksInfo.setImageUrl("https://web-lesson.bj.bcebos.com/guzy-bos/" + fileKey);
        worksInfo.setText(text);
        worksInfo.setUserId(userId);

        boolean b = worksInfoService.updateWorkInfo(worksInfo);
        if (b) {
            return Result.success("作品修改成功");
        } else {
            return Result.failure("作品修改失败");
        }
    }


    @ApiImplicitParams({
            @ApiImplicitParam(value = "token", name = "token", dataType = "String", required = true),
            @ApiImplicitParam(value = "作品id", name = "id", dataType = "Long", required = true)
    })
    @ApiOperation(value = "删除作品")
    @DeleteMapping("delete/{id}")
    public Result deleteWorkInfoById(@PathVariable Long id, @RequestParam String token) {
        boolean b = worksInfoService.deleteWorkInfoById(id);
        if (b) {
            return Result.success("作品删除成功");
        } else {
            return Result.failure("作品删除失败");
        }
    }


    @ApiImplicitParams({
            @ApiImplicitParam(value = "token", name = "token", dataType = "String", required = true),
            @ApiImplicitParam(value = "第几页", name = "currentPage", dataType = "int", required = true),
            @ApiImplicitParam(value = "当前页多少条数据", name = "pageSize", dataType = "int", required = true),
            @ApiImplicitParam(value = "用户id", name = "userId", dataType = "int")
    })
    @ApiOperation(value = "根据用户id查询作品")
    @GetMapping("getAllByUserId/{currentPage}/{pageSize}")
    public Result getWantInfoListByUserId(
            @PathVariable Integer currentPage,
            @PathVariable int pageSize,
            @RequestParam Long userId,
            @RequestParam String token) {
        IPage<WorksInfo> worksInfoIPage = worksInfoService.getWorkInfoListByUserId(currentPage, pageSize, userId);
        if (worksInfoIPage.getRecords().size() > 0) {
            return Result.success(worksInfoIPage);
        } else {
            return Result.failure(RESULT_CODE_DATA_NONE);
        }
    }


    @ApiImplicitParams({
            @ApiImplicitParam(value = "token", name = "token", dataType = "String", required = true),
            @ApiImplicitParam(value = "作品id", name = "worksId", dataType = "Long", required = true)
    })
    @ApiOperation(value = "根据作品id查找")
    @GetMapping("getWorkInfoById/{worksId}")
    public Result getWantInfoById(
            @PathVariable Long worksId,
            @RequestParam String token) {
        Optional<WorksInfo> workInfoById = worksInfoService.getWorkInfoById(worksId);
        if (workInfoById.isPresent()) {
            return Result.success(workInfoById);
        } else {
            return Result.failure(RESULT_CODE_DATA_NONE);
        }
    }

}
